///////////////////////////////////////////////////////////////
/// $Id: byte_tree_test.cc 36 2009-10-13 13:34:25Z tonyjobmails $
/// $Date: 2009-10-13 13:34:25 +0000 (Tue, 13 Oct 2009) $
/// $Author: tonyjobmails $
/// $Revision: 36 $
/// E-mail: tonyjobmails@gmail.com
/// filename: operate_unit_test.cc
/// function: byte树的单元测试
///////////////////////////////////////////////////////////////

#include <algorithm>
#include "bytetree.h"

using namespace tototoy;
using namespace std;

namespace op
{
	///////////////////////////////////////////////////////////////
	/// 筛选答案
	class action_selete_answer : public action<_node_linklist<_node_byte<_character_type>*>*, _node_byte<_character_type>*>
	{
		public:
			action_selete_answer(_uint_16 diff)
			{
				this->max_diff = diff;
			}
			~action_selete_answer() {};
			
			bool operator()(_node_linklist<_node_byte<_character_type>*>* &data)
			{
				if(data && data->data && this->pass_condition(data)
						&& abs(standard_emotion - data->data->data.emotion) < max_diff)
				{
					answer_info tmp;
					tmp.data = data->data;
					tmp.rate = double((max_diff - abs(standard_emotion - data->data->data.emotion)))
						/ double(max_diff) * 100;
#ifdef _TONY_DEBUG
					std::cout << "筛选答案:"
						  << " 标准情绪值(" << standard_emotion << "), "
						  << "此答案情绪值(" << data->data->data.emotion << "), "
						  << "筛选机率(" << tmp.rate << "%)" << std::endl;
#endif
					this->answer_list.push_back(tmp);
					return true;
				}
				return false;
			}
			
			/// 答案信息结构
			struct answer_info
			{
				_node_byte<_character_type>* data;		/// 指向答案
				int rate;					/// 机率
			};
			std::vector<answer_info> answer_list;			/// 答案列表
			_int_16 standard_emotion;				/// 标准对比值
			_uint_16 max_diff;

			static bool sort_dec(const answer_info &_left, const answer_info &_right)
			{
				return _left.rate > _right.rate;
			}
			/// 筛选答案
			_node_byte<_character_type>* select_answer()
			{
				std::vector<answer_info>::iterator beg = this->answer_list.begin();
				std::vector<answer_info>::iterator end = this->answer_list.end();
				if(beg != end)
				{
					stable_sort(this->answer_list.begin(), this->answer_list.end(), action_selete_answer::sort_dec);
				}
				_uint_16 _rand = 9;
				std::vector<answer_info>::iterator pos = beg;
				for(; pos != this->answer_list.end(); ++pos)
				{
#ifdef _TONY_DEBUG
					std::cout << "筛选答案:几率("<< pos->rate << "%)" << std::endl;
#endif
					srand(time(NULL) * pos->rate / _rand);
					if(rand_between(1, 100) <= pos->rate)
						return pos->data;
					_rand *= _rand;
				}
				if(beg != this->answer_list.end())
				{
#ifdef _TONY_DEBUG
					std::cout << "没有找到答案" << endl;
#endif
					return beg->data;
				}
				return NULL;
			}
	};
	///////////////////////////////////////////////////////////////
};

bool insert_rechain_func(char *chain, _int_16 emotion_1, char* rechain, _int_16 emotion_2, byte_tree *tree)
{
	cout << "chain=" << chain << ", rechain=" << rechain << endl;
	if(tree && chain && rechain)
	{
		link_list<_character_type> list_chain, list_rechain;
		_character_type data;

		for(unsigned int pos = 0; pos < strlen(chain) && chain[pos] != 0; ++pos)
		{
			data.data = chain[pos];
			if(pos < strlen(chain) && chain[pos + 1] == 0)
			{
				data.emotion = emotion_1;
			}
			list_chain.insert_back(data);
		}
		for(unsigned int pos = 0; pos < strlen(rechain) && rechain[pos] != 0; ++pos)
		{
			data.data = rechain[pos];
			if(pos < strlen(rechain) && rechain[pos + 1] == 0)
			{
				data.emotion = emotion_2;
			}
			list_rechain.insert_back(data);
		}
		return tree->insert_circle_chain(list_chain, list_rechain);
	}
	return false;
}

int main()
{
	_character_type data;
	link_list<_character_type> list_chain, list_rechain;
	op::action_print<_node_byte<_character_type>*> print;	/// 打印操作接口

	/// 生成一条链和这条链的反链
	char chain[50] = "我爱你";
	char rechain[50] = "我知道";
	for(int pos = 0; pos < 50 && chain[pos] != 0; ++pos)
	{
		data.data = chain[pos];
		if(pos < 50 && chain[pos + 1] == 0)
		{
			data.emotion = 10;
		}
		list_chain.insert_back(data);
	}
	for(int pos = 0; pos < 50 && rechain[pos] != 0; ++pos)
	{
		data.data = rechain[pos];
		if(pos < 50 && rechain[pos + 1] == 0)
		{
			data.emotion = 15;
		}
		list_rechain.insert_back(data);
	}

	byte_tree tree;
	cout << "测试删除树" << endl;
	tree.delete_tree();

/*
	split_line(false);


	cout << "测试insert_circle_chain:" << endl;
	/// 测试插入回型链
	if(tree.insert_circle_chain(list_chain, list_rechain))
	{
		cout << "插入回型链成功" << endl;
	}
	/// byte_tree::traver_tree<_node_byte<_character_type>*>(tree.root, &print);	/// 遍历整个树
	cout << endl;

	/// 测试查找链
	_node_byte<_character_type> *tree_node = tree.search_chain(list_chain);
	if(tree_node)
	{
		cout << "查找链成功" << endl;

		_node_byte<_character_type> *answer_node = NULL;
		if(tree_node->linker)
		{
			if(tree_node->linker->get_front_elem(answer_node) && answer_node)
			{
				/// 测试找导出链
				link_list<_character_type> *list_export;
				list_export = tree.export_chain(answer_node);
				if(list_export)
				{
					cout << "导出链成功: " << list_chain << "->" << *list_export << endl;
				}
			}
			else
			{
				cout << "导出结点linker没有元素" << endl;
			}
		}
		else
		{
			cout << "查找出的结点没有linker" << endl;
		}
	}


	split_line(false);


	cout << "测试第insert_rechain:" << endl;
	bzero(chain, sizeof(chain));
	bzero(rechain, sizeof(rechain));
	strncpy(chain, "你叫什么名字啊?", sizeof(chain));
	strncpy(rechain, "我的名字叫toto, 你呢?", sizeof(rechain));
	list_chain.delete_list();
	list_rechain.delete_list();
	for(int pos = 0; pos < 50 && chain[pos] != 0; ++pos)
	{
		data.data = chain[pos];
		if(pos < 50 && chain[pos + 1] == 0)
		{
			data.emotion = -50;
		}
		list_chain.insert_back(data);
	}
	for(int pos = 0; pos < 50 && rechain[pos] != 0; ++pos)
	{
		data.data = rechain[pos];
		if(pos < 50 && rechain[pos + 1] == 0)
		{
			data.emotion = 251;
		}
		list_rechain.insert_back(data);
	}
	_node_byte<_character_type> *tmp_node = NULL;
	if(tree.insert_chain(list_chain, tmp_node))
	{
		cout << "插入链(" << list_chain << ")成功!" << endl;
		if(tree.insert_rechain(list_chain, list_rechain))
		{
			cout << "插入反链(" <<list_rechain << ")成功!" << endl;
			_node_byte<_character_type> *tree_node = tree.search_chain(list_chain);
			if(tree_node)
			{
				cout << "查找链成功" << endl;

				_node_byte<_character_type> *answer_node = NULL;
				if(tree_node->linker)
				{
					if(tree_node->linker->get_front_elem(answer_node) && answer_node)
					{
						/// 测试找导出链
						link_list<_character_type> *list_export;
						list_export = tree.export_chain(answer_node);
						if(list_export)
						{
							cout << "导出链成功: " << list_chain << "->" << *list_export << endl;
						}
					}
					else
					{
						cout << "导出结点linker没有元素" << endl;
					}
				}
				else
				{
					cout << "查找出的结点没有linker" << endl;
				}
			}
		}
	}


	split_line(true);
*/

	cout << "测试筛选答案:" << endl;
	list_chain.delete_list();
	list_rechain.delete_list();

	bzero(chain, sizeof(chain));
	strncpy(chain, "你叫什么名字?", sizeof(chain));
	for(int pos = 0; pos < 50 && chain[pos] != 0; ++pos)
	{
		data.data = chain[pos];
		if(pos < 50 && chain[pos + 1] == 0)
		{
			data.emotion = -1;
		}
		list_chain.insert_back(data);
	}

	if(insert_rechain_func("你叫什么名字?", -1, "我好不认识你吧？", -7, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "妈的, 老子又不认识你, 滚到一边去!", -12, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "你是谁? 我为什么要告诉你?", -8, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "我叫toto, 您呢? 我可以知道您的名字吗", 14, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "我啊, 叫toto, 你呢?", 11, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "我叫toto, 什么事?", 8, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "有事吗?你是哪位?", 7, &tree)
			&& insert_rechain_func("你叫什么名字?", -1, "你谁啊你, 我叫什么名字关你什么事!", -9, &tree))
	{
		cout << "插入问题成功:" << list_chain << endl;
		_node_byte<_character_type> *tree_node = tree.search_chain(list_chain);
		if(tree_node)
		{
			cout << "查找链成功" << endl;

			cout << "首先打印所有可能答案:" << endl;

			_node_byte<_character_type> *answer_node = NULL;
			if(tree_node->linker)
			{
				_uint_16 idx = 1;
				while(idx <= tree_node->linker->get_length())
				{
					if(tree_node->linker->get_pos_elem(idx, answer_node) && answer_node)
					{
						/// 测试找导出链
						link_list<_character_type> *list_export;
						list_export = tree.export_chain(answer_node);
						if(list_export)
						{
							cout << "导出链成功: " << list_chain << "->" << *list_export << endl;
						}
					}
					else
					{
						cout << "导出结点linker没有元素" << endl;
					}
					++idx;
				}
			}
			else
			{
				cout << "查找出的结点没有linker" << endl;
			}


			if(tree_node->linker)
			{
				action_selete_answer action(10);
				action.standard_emotion = tree_node->data.emotion;
				tree_node->linker->traver_node<_node_byte<_character_type>*>(&action);
				answer_node = action.select_answer();
				if(answer_node)
				{
					/// 测试找导出链
					link_list<_character_type> *list_export;
					list_export = tree.export_chain(answer_node);
					if(list_export)
					{
						cout << "导出链成功: " << list_chain << "->" << *list_export << endl;
					}
				}
				else
				{
					cout << "导出结点没有找到答案" << endl;
				}
			}
			else
			{
				cout << "查找出的结点没有linker" << endl;
			}
		}
	}
	else
	{
		cout << "插入环链不成功" << endl;
	}


	split_line(true);

/*
	/// 测试删除链
	if(tree.delete_chain(list_rechain))
	{
		cout << "删除链成功" << endl;
	}
	byte_tree::traver_tree<_node_byte<_character_type>*>(tree.root, &print);	/// 遍历整个树
	cout << endl;

	tree.delete_tree();
*/
	return 0;
}
